<?php

/**
 * order actions.
 *
 * @package    briefingPropel
 * @subpackage order
 * @author     Your name here
 */
class orderActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->categories = TextcategoriesPeer::getWithOrders();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->order = $this->getRoute()->getObject();
//    $this->order = new Clientorder();//
//    $this->order->setOrdername($this->getRoute()->getObject()->getOrdername());
  }

  public function executeNew(sfWebRequest $request)
  {
    
    $this->form = new ClientorderForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
//    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new ClientorderForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
//    $this->forward404Unless($Clientorder = ClientorderPeer::retrieveByPk($request->getParameter('idorder')), sprintf('Object Clientorder does not exist (%s).', $request->getParameter('idorder')));
//    $this->form = new ClientorderForm($Clientorder);

    $this->form = new ClientorderForm($this->getRoute()->getObject());
  }

  public function executeUpdate(sfWebRequest $request)
  {
//    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
//    $this->forward404Unless($Clientorder = ClientorderPeer::retrieveByPk($request->getParameter('idorder')), sprintf('Object Clientorder does not exist (%s).', $request->getParameter('idorder')));
//    $this->form = new ClientorderForm($Clientorder);
    
    $this->form = new ClientOrderForm($this->getRoute()->getObject());
    
    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

//    $this->forward404Unless($Clientorder = ClientorderPeer::retrieveByPk($request->getParameter('idorder')), sprintf('Object Clientorder does not exist (%s).', $request->getParameter('idorder')));
    
    $Clientorder = $this->getRoute()->getObject();
    $Clientorder->delete();

    $this->redirect('order/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    
    //bind the embedded forms
      //other text media
    $form->getEmbeddedForm('Other text media')->bind(
            $request->getParameter($form->getEmbeddedForm('Other text media')->getName()),
            $request->getFiles($form->getEmbeddedForm('Other text media')->getName())
    );
      //edition options
    $form->getEmbeddedForm('Edition options')->bind(
            $request->getParameter($form->getEmbeddedForm('Edition options')->getName()),
            $request->getFiles($form->getEmbeddedForm('Edition options')->getName())
    );
      //free text
    $form->getEmbeddedForm('Free text')->bind(
            $request->getParameter($form->getEmbeddedForm('Free text')->getName()),
            $request->getFiles($form->getEmbeddedForm('Free text')->getName())
    );

    $form->bind(
            $request->getParameter($form->getName()),
            $request->getFiles($form->getName())
    );

    if ($form->isValid()) 
    {
      $Clientorder = $form->save();

      $this->redirect($this->generateUrl('order_show_order', $Clientorder));
    }
  }
}
